home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2882 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: why get 9 strings only?
  5. Date: Wed, 24 Jan 96 17:25:37 GMT
  6. Organization: none
  7. Message-ID: <822504337snz@genesis.demon.co.uk>
  8. References: <4e51th$4bi@news.nevada.edu> <4e5km4$2e6@news.iag.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4e5km4$2e6@news.iag.net> jatmon@iag.net "John R Buchan" writes:
  15.  
  16. >In article <4e51th$4bi@news.nevada.edu>, chancl@nevada.edu says...
  17. >>
  18. >>I can't figure out why I can only input 9 strings
  19. >>instead of 10 on the following program:
  20.  
  21. >>{
  22. >>        char str[10][80];
  23. >>        int i, j;
  24. >>
  25. >>        printf("Enter a number (0-9):\n");
  26. >>        scanf("%d", &j);
  27. >
  28. >Using scanf for input to input numeric values is tricky.  Here you've
  29. >told it to read an integer value and store it in j.  It obediently
  30. >reads chars until it reaches the first one that isn't allowed for ints.
  31. >In this case probably a '\n'.  It leaves the unused char in the buffer,
  32. >converts the read chars, and stores the value in j.
  33.  
  34. That's one problem.
  35.  
  36. ...
  37.  
  38. >>
  39. >>        if(j>0 && j<10)
  40. >>                printf("Answer: %s\n", str[j]);
  41. >>}
  42.  
  43. This only allows you to select indices between 1 and 9 i.e. 9 values. To
  44. see the first one you need to make the first test j>=0. Couples with the
  45. first problem this makes it look like it reads only the first 9 lines
  46. and indexes the first line from 1 rather than 0.
  47.  
  48. -- 
  49. -----------------------------------------
  50. Lawrence Kirby | fred@genesis.demon.co.uk
  51. Wilts, England | 70734.126@compuserve.com
  52. -----------------------------------------
  53.